package cn.edu.nks.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * 时间工具类
 * @author wangbs
 *
 */
public class DateTimeUtil {
	private static final SimpleDateFormat datetimeFormart = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final SimpleDateFormat dateFormart = new SimpleDateFormat("yyyy-MM-dd");
	
	public static long toUtc(Date dateTime){
		return dateTime.getTime() / 1000;
	}
	
	public static Date toDate(long second){
		return new Date( (long)second * 1000);
	}
	
	public static Date dateNow(){
		Calendar date = Calendar.getInstance();
		date.setTime(new Date());
		date.set(Calendar.HOUR_OF_DAY, 0);
		date.set(Calendar.MINUTE, 0);
		date.set(Calendar.SECOND, 0);
		date.set(Calendar.MILLISECOND, 0);
		
		return date.getTime();
	}
	
	
	/**
	 * 添加天数
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date add(Date date, int days){
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.DAY_OF_MONTH, days);
		ca.set(Calendar.HOUR_OF_DAY, 0);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);
		ca.set(Calendar.MILLISECOND, 0);
		
		return ca.getTime();
	}
	
	/**
	 * 添加分钟
	 * @param date
	 * @param minutes
	 * @return
	 */
	public static Date addMinutes(Date date, int minutes){
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.add(Calendar.MINUTE, minutes);
		return ca.getTime();
	}
	
	public static Date endTime(Date date){
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.set(Calendar.HOUR_OF_DAY, 23);
		ca.set(Calendar.MINUTE, 59);
		ca.set(Calendar.SECOND, 59);
		
		return ca.getTime();
	}
	
	public static Date dateNext(){
		Calendar date = Calendar.getInstance();
		date.setTime(new Date());
		date.add(Calendar.DAY_OF_MONTH, 1);
		date.set(Calendar.HOUR_OF_DAY, 0);
		date.set(Calendar.MINUTE, 0);
		date.set(Calendar.SECOND, 0);
		date.set(Calendar.MILLISECOND, 0);
		
		return date.getTime();
	}
	
	public static Date datePrevious(){
		Calendar date = Calendar.getInstance();
		date.setTime(new Date());
		date.add(Calendar.DAY_OF_MONTH, -1);
		date.set(Calendar.HOUR_OF_DAY, 0);
		date.set(Calendar.MINUTE, 0);
		date.set(Calendar.SECOND, 0);
		date.set(Calendar.MILLISECOND, 0);
		
		return date.getTime();
	}
	
	public static Date toDate(String dateString) throws ParseException{
		if(dateString == null || dateString.length() < 1)
			throw new ParseException("数据为null", 0);
		
		return dateFormart.parse(dateString);
	}
	
	public static Date toDateTime(String dateString) throws ParseException{
		if(dateString == null || dateString.length() < 1)
			throw new ParseException("数据为null", 0);
		
		return datetimeFormart.parse(dateString);
	}
	
	public static Date convert(int minutes){
		Calendar date = Calendar.getInstance();
		date.setTime(new Date());
		date.set(Calendar.HOUR_OF_DAY, minutes / 60);
		date.set(Calendar.MINUTE, minutes % 60);
		date.set(Calendar.SECOND, 0);
		date.set(Calendar.MILLISECOND, 0);
		
		return date.getTime();
	}
	
	public static Date convert(Date day, int minutes){
		Calendar date = Calendar.getInstance();
		date.setTime(day);
		date.set(Calendar.HOUR_OF_DAY, minutes / 60);
		date.set(Calendar.MINUTE, minutes % 60);
		date.set(Calendar.SECOND, 0);
		date.set(Calendar.MILLISECOND, 0);
		
		return date.getTime();
	}
	
	public static String toDateTimeString(Date dateTime){
		if(dateTime == null)
			return null;
		
		return datetimeFormart.format(dateTime);
	}
	
	public static String toDateString(Date date){
		if(date == null)
			return null;
		
		return dateFormart.format(date);
	}
	
	public static int getMinutes(Date time){
		if(time == null)
			return 0;
		
		Calendar ca = Calendar.getInstance();
		ca.setTime(time);
		
		return ca.get(Calendar.HOUR_OF_DAY) * 60 + ca.get(Calendar.MINUTE);
	}
	
	public static int getSeconds(Date time){
		if(time == null)
			return 0;
		
		return (int)(time.getTime() / 1000);
	}
	
	/**
	 * 获取秒
	 * @param begintime
	 * @param endtime
	 * @return
	 */
	public static int getSeconds(Date begintime, Date endtime){
		if(begintime == null || endtime == null)
			return 0;
		
		return (int)((endtime.getTime() - begintime.getTime()) / 1000);
	}
	
	public static String toDateTimeString(String format, Date datetime){
		if(datetime == null)
			return "";
		
		SimpleDateFormat dateFormart;
		if(format == null || format.length() < 1)
			dateFormart = datetimeFormart;
		else
			dateFormart = new SimpleDateFormat(format);
		
		return dateFormart.format(datetime);
	}

}
